package com.wxg.chapter03.jdk;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * date : 2018年08月10日
 *
 * 按照这里的代码的结果来看
 *      |- Random rand = new Random();  可以定义成一个 `static final` 类型的成员变量。
 *      |- rand.nextInt(100) 得出的结果都是小于 `100` 的数字。[1, 100)
 */
public class RandomInThread {

    public static final int N = 120;

    private static final Random rand = new Random();

    private void printRandomNum(){
        System.out.println(rand.nextInt(100));
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final RandomInThread rit = new RandomInThread();

        ExecutorService exec = Executors.newCachedThreadPool();
        exec.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("=== 001 run ===");
                for (int i=0;i<N;i++){
                    rit.printRandomNum();
                }
            }
        });
        exec.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("=== 002 run ===");
                for (int i=0;i<N;i++){
                    rit.printRandomNum();
                }
            }
        });
        exec.shutdown();
    }
}
